﻿using System;
using System.ComponentModel;
using System.Net;
using System.Windows;
using Labthink.PCSoft.UniversalHost4.Utility;
using SystemSettings;
using Labthink.PCSoft.UniversalHost4.Model.Setting;

namespace Labthink.PCSoft.UniversalHost4.UserControls
{
    public partial class UcDataShield
    {
        private NotifyManager _notify;
        public UcDataShield()
        {
            InitializeComponent();
            BtnOk.Focus();
        }

        private void UcDataShield_OnLoaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
            {
                return;
            }

            var w = Window.GetWindow(this);
            _notify = NotifyManager.InstanceManager(w);

            _triggerButtonsStatus(false);
            //var sd = await InstrumentManager.CurrentInstrument.Commands.GetDsInfoAsync();
            //if (sd.ErrorCode < 0)
            {
                //MessageBox.Show(sd.ErrorMsg);
                //_notify.ShowError(sd.ErrorMsg);
            }
            //else
            {
                //var r = sd.Value;
                //if (!string.IsNullOrEmpty(r))
                {
                    //var ds = JsonConvert.DeserializeObject<DataShieldInfo>(r);
                    var ds = ConfigAndSettings.GetDataShieldInfo();
                    TbDsIp.Value = ds.Ip + "";
                    TbDsPort.Value = ds.Port + "";
                    CbAutoDelete.IsChecked = ds.AutoDelete > 0;
                }
            }

            _triggerButtonsStatus(true);
        }

        public string Title { get; set; } = LanguageManager.GetLanguageValue("DataShieldSetting");
        public string PermissionCode { get; set; } = "Per_DataShieldSetting";

        private void btnDSTest_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(TbDsIp.Value) || string.IsNullOrEmpty(TbDsPort.Value)) return;
            var ip = TbDsIp.Value.Trim();
            var port = TbDsPort.Value.ToInt32();

            BtnOk.IsEnabled = false;
            BtnDsTest.IsEnabled = false;
            try
            {
                var socket = new System.Net.Sockets.TcpClient();
                var ir = socket.BeginConnect(IPAddress.Parse(ip), port, null, null);
                ir.AsyncWaitHandle.WaitOne(300, true);
                if (socket.Connected)
                {
                    _notify.ShowSuccess(LanguageManager.GetLanguageValue("连接成功"));
                    socket.EndConnect(ir);
                    BtnDsTest.IsEnabled = true;
                    BtnOk.IsEnabled = true;
                }
                else
                {
                    _notify.ShowError(LanguageManager.GetLanguageValue("连接失败"));
                    socket.Close();
                    BtnDsTest.IsEnabled = true;
                }
            }
            catch (Exception)
            {
                _notify.ShowError(LanguageManager.GetLanguageValue("连接失败"));
                BtnDsTest.IsEnabled = true;
            }
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(TbDsIp.Value) || string.IsNullOrEmpty(TbDsPort.Value)) return;

            _triggerButtonsStatus(false);

            var ds = new DataShieldInfo();
            ds.Ip = TbDsIp.Value;
            ds.Port = string.IsNullOrEmpty(TbDsPort.Value) ? 10001 : TbDsPort.Value.ToInt();
            if (CbAutoDelete.IsChecked.HasValue)
            {
                ds.AutoDelete = CbAutoDelete.IsChecked.Value ? 1 : 0;
            }
            else
            {
                ds.AutoDelete = 0;
            }

            ConfigAndSettings.SetDataShieldInfo(ds);

            _triggerButtonsStatus(true);
        }

        private void _triggerButtonsStatus(bool status)
        {
            BtnOk.IsEnabled = status;
            BtnDsTest.IsEnabled = status;
            Loading.Visibility = status ? Visibility.Collapsed : Visibility.Visible;
        }
    }
}
